//-----------------------------------------------------------------------
// <copyright file="a_SessionDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_SessionDAOBase
    {
        #region Common methods
        public virtual a_Session Createa_SessionFromReader(IDataReader reader)
        {
            a_Session item = new a_Session();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SessionID"))) item.SessionID = (string)reader["SessionID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Time_stamp"))) item.Time_stamp = (DateTime)reader["Time_stamp"];
                if (!reader.IsDBNull(reader.GetOrdinal("CookieID"))) item.CookieID = (string)reader["CookieID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Login_Status"))) item.Login_Status = (bool)reader["Login_Status"];
                if (!reader.IsDBNull(reader.GetOrdinal("RemoteIP"))) item.RemoteIP = (string)reader["RemoteIP"];
                if (!reader.IsDBNull(reader.GetOrdinal("Referrer"))) item.Referrer = (string)reader["Referrer"];
                if (!reader.IsDBNull(reader.GetOrdinal("Hits"))) item.Hits = (int)reader["Hits"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Session methods
            
        public virtual void Createa_Session(a_Session a_Session)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sessions_ins");
                
                database.AddInParameter(dbCommand, "@SessionID", DbType.AnsiString, a_Session.SessionID);
                database.AddInParameter(dbCommand, "@Time_stamp", DbType.DateTime, a_Session.Time_stamp);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_Session.CookieID);
                database.AddInParameter(dbCommand, "@Login_Status", DbType.Boolean, a_Session.Login_Status);
                database.AddInParameter(dbCommand, "@RemoteIP", DbType.AnsiString, a_Session.RemoteIP);
                database.AddInParameter(dbCommand, "@Referrer", DbType.AnsiString, a_Session.Referrer);
                database.AddInParameter(dbCommand, "@Hits", DbType.Int32, a_Session.Hits);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual void Createa_Session(a_Session a_Session,out bool IsOk)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sessions_insByCheckCkid");

                database.AddInParameter(dbCommand, "@SessionID", DbType.AnsiString, a_Session.SessionID);
                database.AddInParameter(dbCommand, "@Time_stamp", DbType.DateTime, a_Session.Time_stamp);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_Session.CookieID);
                database.AddInParameter(dbCommand, "@Login_Status", DbType.Boolean, a_Session.Login_Status);
                database.AddInParameter(dbCommand, "@RemoteIP", DbType.AnsiString, a_Session.RemoteIP);
                database.AddInParameter(dbCommand, "@Referrer", DbType.AnsiString, a_Session.Referrer);
                database.AddInParameter(dbCommand, "@Hits", DbType.Int32, a_Session.Hits);

                database.ExecuteNonQuery(dbCommand);
                IsOk = (bool)database.GetParameterValue(dbCommand, "@IsOk");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Session methods
        
        public virtual void Updatea_Session(a_Session a_Session)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sessions_upd");            
                
                database.AddInParameter(dbCommand, "@SessionID", DbType.AnsiString, a_Session.SessionID);
                database.AddInParameter(dbCommand, "@Time_stamp", DbType.DateTime, a_Session.Time_stamp);
                database.AddInParameter(dbCommand, "@CookieID", DbType.AnsiString, a_Session.CookieID);
                database.AddInParameter(dbCommand, "@Login_Status", DbType.Boolean, a_Session.Login_Status);
                database.AddInParameter(dbCommand, "@RemoteIP", DbType.AnsiString, a_Session.RemoteIP);
                database.AddInParameter(dbCommand, "@Referrer", DbType.AnsiString, a_Session.Referrer);
                database.AddInParameter(dbCommand, "@Hits", DbType.Int32, a_Session.Hits);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Session methods
        public virtual void Deletea_Session(string sessionID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sessions_del");
                
                database.AddInParameter(dbCommand, "@SessionID", DbType.AnsiString, sessionID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Session methods
        
        public virtual a_Session Geta_Session(string sessionID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sessions_sel");
                
                database.AddInParameter(dbCommand, "@SessionID", DbType.AnsiString, sessionID);
                
                a_Session a_Session =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Session = Createa_SessionFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Session;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_SessionList methods
        public virtual a_SessionCollection Geta_SessionList(a_SessionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Sessions_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_SessionCollection a_SessionCollection = new a_SessionCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Session a_Session = Createa_SessionFromReader(reader);
                        a_SessionCollection.Add(a_Session);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_SessionCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_SessionCollection Geta_SessionList(a_SessionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_SessionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}